package Utility;


import Context.Context;
import Context.ContextHolder;

/**
 * @category Utility module
 * @author Sean Lionheart
 * @version 1.0.0.0
 * The level filter.
 */
public class LogFilterLevel extends LogFilterEnable {
	private int minLevel;
	private int maxLevel;
	
	/**
	 * Initialize the filter according to the configuration.
	 */
	public LogFilterLevel() {
		Context ctxContext = ContextHolder.getContext();
		String maxLevelString = ctxContext.getProperty(LogContext.LOGLEVELMAX);
		String minLevelString = ctxContext.getProperty(LogContext.LOGLEVELMIN);
		
		try {
			minLevel = Integer.parseInt(minLevelString);
			maxLevel = Integer.parseInt(maxLevelString);
		}
		//When the configuration is corrupted.
		catch (NumberFormatException e) {
			maxLevel = 5;
			minLevel = 0;
		}
	}
	/**
	 * Only the level of the entry is between the max and minimum values can pass the filter.
	 */
	public boolean filtrateLog(LogEntry entry) {
		int logCategory = entry.getLogCategory();
		return (enable && logCategory >= minLevel && logCategory <= maxLevel);
	}
}
